package com.itheima.web.servlet;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import com.itheima.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {
    /*    public void checkName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            //获取姓名
            String name = req.getParameter("name");
            //调用service查询
            UserService service = new UserServiceImpl();
            int count = service.findByName(name);
            PrintWriter writer = resp.getWriter();
            //判断 如果count为 0 那么姓名可以使用  1  那么姓名不可以用
            if (count==0){
                writer.write("<font color='green'>姓名可用</font>");
            }else{
                writer.write("<font color='red'>姓名已存在, 不可用</font>");
            }

        }*/
    public void checkName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取姓名
        String name = req.getParameter("name");
        //调用service
        UserService service = new UserServiceImpl();
        PrintWriter writer = resp.getWriter();
        int count = service.findByName(name);
        //判断
        if (count==0){
            writer.write("\"<font color='green'>姓名可用</font>\"");
        }else{
            writer.write("<font color='red'>姓名已存在, 不可用</font>");
        }
    }

    public void deleteAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取删除的 id
        String[] ids = req.getParameterValues("id");
        //调用service删除
        UserService service = new UserServiceImpl();
        service.deleteAll(ids);
        resp.sendRedirect(req.getContextPath() + "/userServlet?method=findAll");

    }

    public void update(HttpServletRequest request, HttpServletResponse response) {

        try {
            //1. 设置请求编码
            request.setCharacterEncoding("UTF-8");
            //2. 获取请求参数Map
            Map<String, String[]> map = request.getParameterMap();
            //3. 封装数据
            User user = new User();
            BeanUtils.populate(user, map);
            //4.调用service保存
            UserService service = new UserServiceImpl();
            service.update(user);
            //5.重定向再次查询
            response.sendRedirect(request.getContextPath() + "/userServlet?method=findAll");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取修改id
        String id =
                request.getParameter("id");

        //2. 调用service查询
        UserService service = new UserServiceImpl();
        User user = service.findById(id);

        //3. 将user添加到请求域中, 转发到update.jsp 回显
        request.setAttribute("user", user);
        request.getRequestDispatcher("/update.jsp").forward(request, response);
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.获取删除用户id
        String id = request.getParameter("id");

        //2. 调用service删除
        UserService service = new UserServiceImpl();
        service.deleteById(id);

        //3. 重定向到 userListServlet 再次查询
        response.sendRedirect(request.getContextPath() + "/userServlet?method=findAll");
    }

    public void save(HttpServletRequest request, HttpServletResponse response) {
        try {
            //1. 设置请求编码  要在获取参数之前设置才可以
            request.setCharacterEncoding("UTF-8");

            //2. 获取所有请求参数map集合
            Map<String, String[]> map = request.getParameterMap();

            //3. 使用BeanUtils, 将map集合封装到User对象
            User user = new User();
            BeanUtils.populate(user, map);

            //4. 调用service保存用户
            UserService service = new UserServiceImpl();
            service.save(user);

            //5. 重定向到 userListServlet 再次查询  (重定向之后浏览器地址栏发生了改变, 防止表单重复提交)
            response.sendRedirect(request.getContextPath() + "/userServlet?method=findAll");
            //request.getRequestDispatcher("/userListServlet").forward(request,response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //0. 获取分页参数
        String currentPageStr = request.getParameter("currentPage");  //
        int currentPage = 1;  // "2"  null  "中文"
        try {
            currentPage = Integer.parseInt(currentPageStr);
        } catch (NumberFormatException e) {
            // e.printStackTrace();
        }
        int pageSize = 5; // 每页显示条数 (后期由页面传递, 临时写的假数据
        //1. 调用service查询用户集合
        UserService service = new UserServiceImpl();
        PageInfo<User> info = service.findAll(currentPage, pageSize);
        //2. 将集合保存到请求域中
        request.setAttribute("pb", info);
        //System.out.println(info);
        //3. 转发到list.jsp 显示数据
        request.getRequestDispatcher("/list.jsp").forward(request, response);


    }


}
